﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Orf.Model;
using Orf.dto;
using Orf_system.收费业务;

namespace Orf_system
{
    public partial class FrmhuaJiaSf : Form
    {

        huaJaShoufeiDAO hjsfDAO = null;
        public static string fphm = null, mzhm = null, Fsum = null;
        public FrmhuaJiaSf()
        {
            hjsfDAO = new huaJaShoufeiDAO();
            InitializeComponent();
        }

        /// <summary>
        /// 把所有门诊号码与病人姓名填充到dataGridView1中
        /// </summary>
        private void GetAllData()
        {
            DataTable dt = null;
            try
            {
                dt = hjsfDAO.GetAllMzhm();
                dataGridView1.DataSource = dt.DefaultView;
                if (dt.Rows.Count >= 0)
                {
                    dataGridView1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void selectDataByMzhm(string strValue)
        {
            DataTable dt = null;
            MZ_GHMX ghmxClass = new MZ_GHMX();
            ghmxClass.mZHM = strValue;
            try
            {
                dt = hjsfDAO.GetMzhmByInput(ghmxClass);
                dataGridView1.DataSource = dt.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 获取发票号码
        /// </summary>
        private void Get_currFphm() //有问题：员工编号获取不了，有待解决
        {
            DataTable dt_currFphm = null;
            DIC_YGDM empClass = new DIC_YGDM();
            empClass.yGDM = FrmLogin.emp_sn;
            try
            {
                dt_currFphm = hjsfDAO.Get_Fphm(empClass, 1);
                if (dt_currFphm.Rows.Count>=0)
                {
                    txtFphm.Text = dt_currFphm.Rows[0][2].ToString();
                }
                fphm = txtFphm.Text;
                //else
                //{
                //    return;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 统计费用
        /// </summary>
        private void GetFy()
        {
            DataTable dt_Fy = null;
            MZ_GHMX ghmxClass = new MZ_GHMX();
            ghmxClass.mZHM = txtMzhm.Text;
            try
            {
                dt_Fy = hjsfDAO.GetFy_fromview(ghmxClass);
                if (dt_Fy.Rows.Count>=0)
                {
                    txtXyfJe.Text = dt_Fy.Rows[0][1].ToString();
                    txtZyfJe.Text = dt_Fy.Rows[0][2].ToString();
                    txtCyfJe.Text = dt_Fy.Rows[0][3].ToString();
                    txtYjdJe.Text = dt_Fy.Rows[0][4].ToString();
                    txtSumJe.Text = dt_Fy.Rows[0][0].ToString();
                }
                Fsum = txtSumJe.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 获取病人基本信息和处方信息
        /// </summary>
        private void Get_PatientBasicInfoAndCfInfo()
        {
            MZ_GHMX ghmxClass = new MZ_GHMX();//声明挂号明细实体类的对象
            ghmxClass.mZHM = txtMzhm.Text;//将输入的门诊号码传到实体类字段中
            mzhm = txtMzhm.Text;
            DataTable dt_BrInfo = null, dt_BrCfInfo = null;//声明两个DataTable对象，分别存病人基本信息和处方信息
            try
            {
                //调用业务操作类中的GetShouFeiInfo方法，传入的参数是门诊号码和类型1或2
                dt_BrInfo = hjsfDAO.GetShouFeiInfo(ghmxClass, 1); //获取病人基本信息
                dt_BrCfInfo = hjsfDAO.GetShouFeiInfo(ghmxClass, 2);//获取病人的处方信息

                //以下是把获取的病人基本信息填充到相应的控件内
                if (dt_BrInfo.Rows.Count >= 0)
                {
                    txtPatientName.Text = dt_BrInfo.Rows[0][0].ToString();
                    txtBrxz.Text = dt_BrInfo.Rows[0][1].ToString();
                    txtBrCardNo.Text = dt_BrInfo.Rows[0][2].ToString();
                    txtMoneyNum.Text = dt_BrInfo.Rows[0][3].ToString();
                    txtDocName.Text = dt_BrInfo.Rows[0][6].ToString();
                    txtDeptName.Text = dt_BrInfo.Rows[0][7].ToString();
                }

                //获取dt_BrCfInfo的行数和列数
                int intRows = dt_BrCfInfo.Rows.Count;
                int intColumns = dt_BrCfInfo.Columns.Count;
                //根据intRows行数，增加DataGridView的行数
                for (int i = 0; i < intRows; i++)
                    dgvCfInfo.Rows.Add();
                //通过双重for语句，把dt_BrCfInfo存的记录显示在DataGridView中
                for (int i = 0; i < intRows; i++)
                {
                    for (int j = 0; j < intColumns; j++)
                    {
                        dgvCfInfo.Rows[i].Cells[j].Value = dt_BrCfInfo.Rows[i][j].ToString();

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        /// <summary>
        /// 按箭头下，聚焦到dataGridView1，然后按上下箭头，选取门诊号码
        /// </summary>
        private void UpandDown()
        {
            try
            {
                this.dataGridView1.Focus();
                if (dataGridView1.CurrentRow.Index + 1 < dataGridView1.Rows.Count)
                {
                    dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.CurrentRow.Index];
                }
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtMzhm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                UpandDown();
            }
            if (e.KeyCode == Keys.Enter)
            {
                try  
                {
                    if (dataGridView1.Visible == true)
                    {
                        txtMzhm.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                        dataGridView1.Visible = false;
                        if (dgvCfInfo.Rows.Count >= 0)
                        {
                            dgvCfInfo.Rows.Clear();
                        }
                        Get_PatientBasicInfoAndCfInfo();//调用获取病人基本信息和处方信息方法
                        GetFy();//统计费用方法
                        Get_currFphm();//调用获取发票号码方法；
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

        private void FrmhuaJiaSf_Load(object sender, EventArgs e)
        {
            dgvCfInfo.Columns[0].Width = 120;//设置处方信息表格的名称这一列的宽度
        }

        private void txtMzhm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 32)
            {
                this.dataGridView1.Location = new Point(91, 41);//让dataGridView显示在文本框门诊号码的下面
                GetAllData();
            }
        }

        private void txtMzhm_TextChanged(object sender, EventArgs e)
        {

            selectDataByMzhm(txtMzhm.Text.Trim());
        }

        /// <summary>
        /// 双击dataGridView1的当前行的数据，把门诊号码填充到门诊号码文本框中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {//门诊号码
            try
            {
                if (dataGridView1.Visible == true)
                {
                    txtMzhm.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    dataGridView1.Visible = false;
                    if (dgvCfInfo.Rows.Count >= 0)
                    {
                        dgvCfInfo.Rows.Clear();
                    }
                    Get_PatientBasicInfoAndCfInfo();//调用获取病人基本信息和处方信息方法
                    GetFy();//统计费用方法
                    Get_currFphm();//调用获取发票号码方法；
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void dgvCfInfo_Click(object sender, EventArgs e)//单击数据体dgvCfInfo时，隐藏dataGridView1
        {
            this.dataGridView1.Visible = false;
        }

        private void FrmhuaJiaSf_Click(object sender, EventArgs e)//单击窗体时，隐藏dataGridView1
        {
            this.dataGridView1.Visible = false;
        }

        private void btnJs_Click(object sender, EventArgs e)
        {
            if (txtMzhm.Text=="")
            {
                MessageBox.Show("请选择一个病人！");
                return;
            }
            else if (dgvCfInfo.Rows.Count<0)
            {
                MessageBox.Show("该病人没有处方信息！");
                return;
            }
            else
            {
                FrmShoufeiJiesuan frmSfjs = new FrmShoufeiJiesuan();
                frmSfjs.ShowDialog();
            }
          
        }

        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==13)
            {
                if (dataGridView1.Visible == true)
                {
                    txtMzhm.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    dataGridView1.Visible = false;
                    if (dgvCfInfo.Rows.Count >= 0)
                    {
                        dgvCfInfo.Rows.Clear();
                    }
                    Get_PatientBasicInfoAndCfInfo();//调用获取病人基本信息和处方信息方法
                    GetFy();//统计费用方法
                    Get_currFphm();//调用获取发票号码方法；
                }
            }
        }
    }
}
